/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.puzzud.engine.core;

import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.geom.Area;

/**
 *
 * @author puzzud
 */
public class Item2D extends Entity
{
    protected Point position;
    protected int width, height;
    protected float angle;

    public Item2D()
    {
        super();

        position = new Point( 0, 0 );
        width = -1;
        height = -1;
        angle = 0.0f;
    }

    public Point getPosition()
    {
        return( position );
    }

    public int getXPosition()
    {
        return( position.x );
    }

    public int getYPosition()
    {
        return( position.y );
    }

    public void setPosition( Point position )
    {
        this.position = position;
    }

    public void setPosition( int xPosition, int yPosition )
    {
        position.x = xPosition;
        position.y = yPosition;
    }

    public void setXPosition( int xPosition )
    {
        position.x = xPosition;
    }

    public void setYPosition( int yPosition )
    {
        position.y = yPosition;
    }

    public int getWidth()
    {
        return( width );
    }

    public void setWidth( int width )
    {
        this.width = width;
    }

    public int getHeight()
    {
        return( height );
    }

    public void setHeight( int height )
    {
        this.height = height;
    }

    public float getAngle()
    {
        return( angle );
    }

    public void setAngle( float angle )
    {
        this.angle = angle;
    }

    public boolean detectCollision( Item2D item )
    {
        return( detectCollision( this , item ) );
    }

    /**
      * Item collision Item2D by Item2D.
      */
    public static boolean detectCollision( Item2D item1, Item2D item2 )
    {
        // TODO: this will need to get complicated when items are being rotated.

        if( ( item1 == null ) || ( item2 == null ) )
        {
            return( false );
        }

        // Check to see if these items are rotated or not.
        if( ( item1.angle != 0.0f ) || item2.angle != 0.0f )
        {
            // If even just one is, we need to do polygon.
            Polygon p1 = new Polygon();
            Polygon p2 = new Polygon();
            
            int item1XPosition = item1.getXPosition();
            int item1YPosition = item1.getYPosition();
            int item1Width = item1.getWidth();
            int item1Height = item1.getHeight();
            
            int item2XPosition = item2.getXPosition();
            int item2YPosition = item2.getYPosition();
            int item2Width = item2.getWidth();
            int item2Height = item2.getHeight();
            
            p1.addPoint( item1XPosition, item1YPosition );
            p1.addPoint( item1XPosition + item1Width, item1YPosition );
            p1.addPoint( item1XPosition + item1Width, item1YPosition + item1Height );
            p1.addPoint( item1XPosition, item1YPosition + item1Height );
            
            p2.addPoint( item2XPosition, item2YPosition );
            p2.addPoint( item2XPosition + item2Width, item2YPosition );
            p2.addPoint( item2XPosition + item2Width, item2YPosition + item2Height );
            p2.addPoint( item2XPosition, item2YPosition + item2Height );

            Area a1 = new Area( p1 );
            Area a2 = new Area( p2 );

            a1.intersect(a2);
            return( !a1.isEmpty() );
        }
        else
        {
            // Else we can do faster rectangular collision detection.
            Rectangle r1 = new Rectangle( item1.getXPosition(), item1.getYPosition(), item1.getWidth(), item1.getHeight() );
            Rectangle r2 = new Rectangle( item2.getXPosition(), item2.getYPosition(), item2.getWidth(), item2.getHeight() );

            if( r1.intersects( r2 ) )
            {
                return( true );
            }
            else
            {
                return( false );
            }
        }
    }
}
